home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_06 / allison / shell.c < prev    next >
C/C++ Source or Header  |  1994-04-08  |  966b  |  53 lines

  1. LISTING 13 - A skeleton for a command interpreter - illustrates
  2. branching out of a signal handler
  3.  
  4. /* shell.c: A skeleton for a command interpreter */
  5.  
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #include <setjmp.h>
  9. /* etc. */
  10.  
  11. jmp_buf restart;
  12.  
  13. void ctrlc(int sig);
  14.  
  15. main()
  16. {
  17.     /* Install signal handler */
  18.     signal(SIGINT,ctrlc);
  19.  
  20.     /* Return here on keyboard interrupt */
  21.     setjmp(restart);
  22.  
  23.     /* Do any other initialization here... */
  24.  
  25.     for (;;)
  26.     {
  27.         int command_code;
  28.         /* etc... */
  29.  
  30.         /* Read and parse command here... */
  31.  
  32.         /* Execute internal command here */
  33.         switch(command_code)
  34.         {
  35.             case 'Q':
  36.                 return 0;   /* quit */
  37.  
  38.             /*  Lots of cases follow... */
  39.         }
  40.     }
  41. }
  42.  
  43. void ctrlc(int sig)
  44. {
  45.     /* Reinstall handler */
  46.     signal(sig,ctrlc);
  47.  
  48.     /* Jump back to command line */
  49.     longjmp(restart,1);
  50.     return;
  51. }
  52.  
  53.